<?php
include_once('Sqlexecutor.php');
class HomePage
{
    private $_dbh;
    private $_logger;

    public function __construct($dbh, $logger = Null)
    {/*{{{*/
        $this->_dbh = $dbh; 
        $this->_logger = $logger; 
    }/*}}}*/

    public function getInfo()
    {/*{{{*/
        $sql = "select `id`, `subject`, `slogan`, `content` from bs_home where `id` = 1";
        try
        {
            $info = $this->_dbh->query($sql);
            return $info;
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return NULL;
        }
    }/*}}}*/

    public function create($subject, $slogan, $content)
    {/*{{{*/
        $sql = "insert into bs_home set `id` = 1, `subject` = ?, `slogan` = ?, `content` = ?";
        $val[] = $subject;
        $val[] = $slogan;
        $val[] = $content;
        try
        {
            $this->_dbh->beginTrans();
            $this->_dbh->execute($sql, $val); 
            $lastId = $this->_dbh->getLastInsertID();
            $this->_dbh->commit();

            return $lastId;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            if($e->errorInfo[1] == 1062 && $e->errorInfo[0] == "23000")
            {
                // Duplicate key
                return -1;
            }
            else
            {
                // Unknow error
                return -100;
            }
        }
    }/*}}}*/

    public function update($subject, $slogan, $content)
    {/*{{{*/
        $sql = 'update bs_home set `subject` = ?, `slogan` = ?, `content` = ? where `id` = 1';
        $val[] = $subject;
        $val[] = $slogan;
        $val[] = $content;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not change, 1 : change 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            if($e->errorInfo[1] == 1062 && $e->errorInfo[0] == "23000")
            {
                // Duplicate key
                return -1;
            }
            else
            {
                // Unknow error
                return -100;
            }
        }
    }/*}}}*/

}

/* example 
$dbh = new Sqlexecutor('localhost', 'tile', 'tile', 'tile');
$s = new HomePage($dbh);
$res = $s->create("abc", "7/b724d9ec0c251d2b7e78df385505f585.jpg");
var_dump($res);
$res = $s->getInfo();
var_dump($res);
$res = $s->create("abcd", "abc");
var_dump($res);
$res = $s->update("abc", "7/b724d9ec0c251d2b7e78df385505f585.jpg");
var_dump($res);
 */
